package com.mall.user.controller;

import com.central.common.model.Result;
import com.central.log.annotation.UserOperationLog;
import com.mall.entity.Users;
import com.mall.user.service.UserCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserCheckController {

    @Autowired
    private UserCheckService userCheckService;

    @GetMapping("/check")
    public Users check(String username){
        return userCheckService.getUser(username);
    }

    @GetMapping("/hello")
    public String hello() {

        return "hello";
    }

    @PostMapping("/save")
    public Result save(@RequestBody Users users) {
        int result = userCheckService.save(users);
        if (result > 0) {
            return Result.succeed("添加成功");
        }
        return Result.failed("添加失败");
    }

    @GetMapping("/all")
    @UserOperationLog(operModul = "用户模块",operType = "查询",operDesc = "查询全部列表")
    public Result getAllUser() {
        List<Users> userList = userCheckService.getUserList();
        if (userList != null) {
            return Result.succeed(userList,"查询成功");
        }
        return Result.failed("查询失败");
    }

    @DeleteMapping("delete")
    public Result userDelete(@RequestParam("userId") String userId) {
        List<Users> usersList = userCheckService.deleteUser(userId);
        if (usersList != null) {
            return Result.succeed(usersList,"删除成功");
        }
        return Result.failed("删除失败");
    }

    @GetMapping("/selectByUsername")
    public Result getByUsername(@RequestParam("username") String username) {
        List<Users> usersList = userCheckService.selectByUsername(username);
        if (usersList != null) {
            return Result.succeed(usersList,"查询成功");
        }
        return Result.failed("查询失败");
    }

}